package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.friends.dao.MessageDao;

import com.stx.friends.entity.MessageEntity;
import com.stx.friends.entity.StudentEntity;
import com.stx.friends.vo.MessageVo;


public class MessageDaoImpl extends JdbcDaoSupport implements MessageDao {

	@Override
	public List<MessageEntity> list(MessageVo vo) {

		String sql = "select * from WRX_MESSAGE";

		JdbcTemplate jt = getJdbcTemplate();

		List<MessageEntity> stuList = jt.query(sql, new MessageRowMapper());

		return stuList;
	}

	class MessageRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			MessageEntity entity = new MessageEntity();

			entity.setMid(rs.getString("mid"));
			entity.setTitle(rs.getString("title"));
			entity.setSenderUid(rs.getString("senderUid"));
			entity.setReceiverUid(rs.getString("receiverUid"));
			entity.setMessage(rs.getString("message"));
			entity.setCreateTime(rs.getString("createTime"));
			entity.setReadFlag(rs.getString("readFlag"));
			entity.setDelFlag(rs.getString("delFlag"));
			entity.setParentId(rs.getString("parentId"));
			return entity;
		}
	}

	@Override
	public void delMessage(String mid) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from WRX_MESSAGE where mid = ?";

		// 构建参数数组
		Object[] args = { mid };

		jt.update(sql, args);

	}

	@Override
	public void addMessage(MessageVo message) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into WRX_MESSAGE(mid,title,senderUid,receiverUid,message,createTime,readFlag,delFlag,parentId) values(seq_wrx_MESSAGE.nextval,?,?,?,?,?,?,?,?)";

		// 构建参数数组
		Object[] args = { message.getMid(), message.getTitle(),
				message.getSenderUid(), message.getReceiverUid(),
				 message.getMessage(), message.getCreateTime(), 
				 message.getReadFlag(), message.getDelFlag(), message.getParentId()};
		
		jt.update(sql, args);

	}
}
